﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace AE.Remoting {
    internal sealed class CacheDictionary {
        public static CacheDictionary<K, T> Create<K, T>(Func<K, T> exec) {
            return new CacheDictionary<K, T>(exec);
        }
    }

    internal class CacheDictionary<K, T> : Dictionary<K, T> {
        private Func<K, T> _exec;
        public CacheDictionary(Func<K, T> exec) {
            _exec = exec;
        }

        public T Get(K key) {
            T result;
            if (!TryGetValue(key, out result)) {
                result = _exec(key);
                lock (this)
                    if (!ContainsKey(key))
                        Add(key, result);
            }

            return result;
        }
    } 
}
